package com.aqa.ms.sample;

/**
 * @author AQA<br />
 * 自动装拆箱——缓存测试
 * <p>
 * [JLS]<br>
 * If the value  p being boxed is an integer literal of type  int between  -128 and  127
 * inclusive (§3.10.1), or the boolean literal  true or  false (§3.10.3), or a character
 * literal between  '\u0000' and  '\u007f' inclusive (§3.10.4), then let  a and  b be the
 * results of any two boxing conversions of  p . It is always the case that  a == b <br/>
 * 如果一个值被装箱，当其是
 * int类型，值在-128~127;
 * boolean类型，值为true或false；
 * character类型，值在'\u0000' ~ '\u007f'(0~127)等情况下，让两个值的装箱类型比较结果为p，则总是 a == b</p>
 * 详情查看：Integer内部私有静态函数 IntegerCache 和 valueOf<br/>
 * 已知原生类型的装箱函数为valueOf，而该函数是在默认情况下，当取值在-128~127之间时，是从缓存数组中取出对象Integer<br/>
 * 可以通过VM参数对最大值进行设置：-XX:AutoBoxCacheMax=<size>
 */
public class BoxedCompare {

    public static void main(String[] args) {

        Integer i1 = new Integer(1);
        Integer i2 = new Integer(1);
        System.out.println(i1 == i2);

        Integer i3 = 1;
        Integer i4 = 1;
        System.out.println(i3 == i4);

        Integer i5 = 127;
        Integer i6 = 127;
        System.out.println(i5 == i6);

        /* 当运行时VM参数进行设置： -XX:AutoBoxCacheMax=255 ，则结果为true*/
        Integer i7 = 128;
        Integer i8 = 128;
        System.out.println(" i7 == i8 ==> " + (i7 == i8));

        Integer i9 = 128;
        Integer i10 = 128;
        System.out.println(i9.equals(i10));

        Integer i11 = 5;
        Long i12 = 6L;
//		System.out.println("i11 == i12 ==> "+ (i11 == i12));	// “==” 号在不遇到算术运算的情况下不会自动拆箱，所以报错
        System.out.println("i11 == i12 ==> " + ((i11 + i3) == i12));

        Integer i13 = 6;
        Long i14 = 6L;
        System.out.println(i13.equals(i14));    //equals函数不会进行数据转型的操作

        /*Character也有做缓存处理*/
        Character a = 'a';
        Character A = 'a';
        System.out.println(" a == A  ==> " + (a == A));


    }


}
